<?php
/**
 * Project: Personal Single PHP Frame
 * @copyright Zhou Baochuan
 * @author Zhou Baochuan
 * @date 4 8th 2011
 * @version 1.0
 */

header("Content-type:text/html; charset=utf-8;");
date_default_timezone_set('Asia/Shanghai');

define('WEB_ROOT',realpath(dirname(__FILE__).'/').'/');
define('APP_ROOT',WEB_ROOT.'app'.'/');
define('DOMAIN','myframe');
define('UPLOAD_ROOT',WEB_ROOT.'upload/');

final class Core{
	public $_module='Default';
	public $_controller = 'Index';
	public $_action = 'index';

	/* singleton{{{*/
	private function __construct(){}
	public static function getInstance(){
		static $instance = NULL;
		if($instance === NULL){
			$instance = new self();
		}
		return $instance;
	}
	final private function __clone(){}
	/*}}}*/

	public function init(){
		$this->initMVC();
		$this->route();
	}

	/**
	 * Initiate MVC
	 */
	private function initMVC(){
		// ====== GET Method Fix Position====== //
		if(isset($_GET['m']) && !empty($_GET['m'])) $this->_module = ucfirst($_GET['m']);
		if(isset($_GET['c']) && !empty($_GET['c'])) $this->_controller= ucfirst($_GET['c']);
		if(isset($_GET['a']) && !empty($_GET['a'])) $this->_action = $_GET['a'];
	}


	// Request Handle{{{
	/**
	 * Request Direction
	 */
	public function route(){
		$f_controller = APP_ROOT.$this->_module.'/class/'.$this->_controller.'.php';

		if(file_exists($f_controller)) 
			include_once $f_controller;
		else 
			Core::showmessage('Controller file not exists');

		// ====== Get Controller Object ====== //
		$controller = $this->factory($this->_controller,'getInstance');

		// ====== Operate Controller Action====== //
		//$controller->{$this->_action}();
		call_user_func(array($controller,$this->_action.'Action'));
	}

	/**
	 * parameterized factory method
	 */
	private  function factory($controller,$method){
		if(class_exists($controller)){
			if(method_exists ($controller,$method)){
				//var_dump($controller,$method);
				return call_user_func(array($controller,$method));
			}elseif(method_exists ($controller,'__construct')){
				return  new $controller;
			}else{
				Core::showmessage('The Method Not Exists');
			}
		}else{
			Core::showmessage('The Current '.$controller.' Class Not Exists');
		}
	}

	
	/*}}}*/

	/* Class Load{{{*/
	// Load The System's Class File
	public function sys_load_class($classname,$instance=true){
		$sub_path='';
		if(strpos($classname,'_')) $sub_path = str_replace('_','/',$classname).'/';
		//$classname = ucfirst($classname);
		$path = WEB_ROOT.'lib/class/'.$sub_path.$classname.'.sys.php';

		static $classes = array(); // 只加载一次
		$key = md5($path);
		if(isset($classes[$key]) && !empty($classes[$key])) return $classes[$key];

		if(file_exists($path)){
			include $path;
			if($instance){
				$app_classes[$key] = new $classname;
				return $app_classes[$key];
			}
		}else{
			Core::showmessage('The Current `'.$path.'` Not Exists');
		}
		return false;
	}

	// Load The Application's Class File
	public function app_load_class($classname,$instance=true){
		//$classname = ucfirst($classname);
		$sub_path=$classname;
		if(strpos($classname,'_')) $sub_path = str_replace('_','/',$classname).'/';
		$path = APP_ROOT.$this->_module.'/model/'.$sub_path.'.php';

		static $app_classes = array(); // 只加载一次
		$key = md5($path);
		if(isset($app_classes[$key]) && !empty($app_classes[$key])) return $app_classes[$key];

		if(file_exists($path)){
			include $path;
			if($instance){
				$app_classes[$key] = new $classname;
				return $app_classes[$key];
			}
		}else{
			Core::showmessage('The Current `'.$path.'` Not Exists');
		}
		return false;

	}
	/*}}}*/

	// Message Handle{{{
	public static function showmessage($msg){
		//throw new Exception ($msg);
		exit($msg);
	}

	public static function sucmessage($msg){
		echo "<script type='text/javascript'>alert('{$msg}');</script>";
	}
	// }}}
	
}
